/**
 *
 * Copyright 2015 RSVP Technologies Inc. All rights reserved.
 * Stats_xySearch.java
 *
 */
package com.gfinal.map.bus;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.gfinal.map.common.Constant;
import com.gfinal.map.util.HttpUtils;

/**
 * @author He Tao (ht@nbicc.com)
 * @date 2015年3月2日
 */
public class Stats_xySearch {
	private String city = null;
	private float lng = 0;
	private float lat = 0;
	private float dist = 0;
	private int result_num = 0;
	private String web_url = "http://bus.aibang.com";
	private String wap_url = "http://wap.aibang.com";
	private ArrayList<Stat_xy_bean> lineList;

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public float getLng() {
		return lng;
	}

	public void setLng(float lng) {
		this.lng = lng;
	}

	public float getLat() {
		return lat;
	}

	public void setLat(float lat) {
		this.lat = lat;
	}

	public float getDist() {
		return dist;
	}

	public void setDist(float dist) {
		this.dist = dist;
	}

	public int getResult_num() {
		return result_num;
	}

	public String getWeb_url() {
		return web_url;
	}

	public String getWap_url() {
		return wap_url;
	}

	@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
	private String createUrl() {
		ArrayList params = new ArrayList();
		params.add(new BasicNameValuePair("app_key", Constant.AIBANG_AK));
		params.add(new BasicNameValuePair("city", city));
		params.add(new BasicNameValuePair("lng", String.valueOf(lng)));
		params.add(new BasicNameValuePair("lat", String.valueOf(lat)));
		params.add(new BasicNameValuePair("dist", String.valueOf(dist)));
		return Constant.BUS_STAT_NEARBY_URL + "?"
				+ URLEncodedUtils.format(params, Constant.CHARSET);
	}

	@SuppressWarnings("rawtypes")
	public ArrayList<Stat_xy_bean> execute() throws IOException {
		lineList = new ArrayList<Stat_xy_bean>();
		String response = HttpUtils.doGet(createUrl());
		SAXReader reader = new SAXReader();

		Document doc;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(
					response.getBytes("UTF-8"));
			doc = reader.read(in);
			Element root = doc.getRootElement(); // 获取根节点
			List<Element> list = new ArrayList<Element>();
			list.add(root);
			while (list.size() > 0) {
				Element element = null;
				Element ele = null;
				Iterator ite = list.iterator();
				if (ite.hasNext()) {
					ele = (Element) ite.next();
					ite.remove();
				}
				if (ele != null) {
					Stat_xy_bean statXyBean = null;
					for (Iterator i = ele.elementIterator(); (i != null)
							&& (i.hasNext());) {
						element = (Element) i.next();
						list.add(element);
						if (element != null) {
							if (element.getName().equals("result_num")) {
								result_num = Integer.valueOf(element.getText());
							} else if (element.getName().equals("name")) {
								statXyBean = new Stat_xy_bean();
								statXyBean.setName(element.getText());
							} else if (element.getName().equals("xy")) {
								statXyBean.setXy(element.getText());
							} else if (element.getName().equals("dist")) {
								statXyBean.setDist(element.getText());
							} else if (element.getName().equals("line_names")) {
								statXyBean.setLine_names(element.getText());
								lineList.add(statXyBean);
							}
						}
					}
				}
				if (lineList.size() == result_num) {
					break;
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < lineList.size(); i++) {
			System.out.println(lineList.get(i).toString());
		}

		return lineList;

	}

	public static void main(String[] args) throws IOException {
		Stats_xySearch nearSearch = new Stats_xySearch();
		nearSearch.setCity("北京");
		nearSearch.setLat(39.99285f);
		nearSearch.setLng(116.33785f);
		nearSearch.setDist(500);
		nearSearch.execute();
	}

}
